extends Node2D

@onready var text1 := $Text1
@onready var text2 := $Text2
@onready var text3 := $Text3
@onready var text4 := $Text4

const FADE_NONE: int = 0
const FADE_IN: int = 1
const FADE_IN_FIN: int = 2
const FADE_IN_TIME: float = 2.0
var elapsed: float = 0
var fade_state = FADE_NONE
var text_list: Array[Label] = []
var current_fade_index: int = 0
var space_pressed: bool = false

func fade_text(delta: float):
	if fade_state == FADE_IN:
		elapsed += delta
		text_list[current_fade_index].modulate.a = min(FADE_IN_TIME, elapsed) / FADE_IN_TIME
		if text_list[current_fade_index].modulate.a >= 1:
			text_list[current_fade_index].modulate.a = 0.5
			current_fade_index += 1
			elapsed = 0
			
			if current_fade_index >= 3:
				fade_state = FADE_IN_FIN

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	text_list.append(text1)
	text_list.append(text2)
	text_list.append(text3)
	for text in text_list:
		text.modulate.a = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	space_pressed = Input.is_action_just_pressed("space")
	if fade_state == FADE_NONE and space_pressed:
		fade_state = FADE_IN
		return
	
	if fade_state == FADE_IN:
		fade_text(delta)
	
	if fade_state == FADE_IN_FIN and space_pressed:
		get_tree().change_scene_to_file("res://karte/karte_1.tscn")
	
	
